package com.jt.handler;

import com.jt.msg.req.AuthMsg;
import com.jt.msg.resp.CommonResp;
import com.jt.utils.TcpChannelManager;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 注册包
 *
 * @author wang xiao
 * date 2023/8/9
 */
@Component
@ChannelHandler.Sharable
public class AuthMsgHandler extends BaseHandler<AuthMsg> {


    private TcpChannelManager channelManager;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AuthMsg msg) throws Exception {
        log.debug(msg.toString());
        channelManager.add(msg.getHeader().getTerminalId(), ctx.channel());
        CommonResp resp = CommonResp.success(msg, getSerialNumber(ctx.channel()));
        write(ctx, resp);
    }


    @Autowired
    public void setChannelManager(TcpChannelManager channelManager) {
        this.channelManager = channelManager;
    }
}
